/**
 * @class   wosh::SharedObject
 * @brief   
 *
 ****************************************************************************
 * @version $Id: SharedObject.h 2676 2010-06-20 02:42:08Z alex $
 * @author  Alessandro Polo
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

#ifndef __WOSH_Core_SharedObject_H__
 #define __WOSH_Core_SharedObject_H__

 #include <woshDefs.h>
 #include <core/Serialization.h>
 #include <map>

				#include <core/URI.h>

namespace wosh {

class SharedMemory;
class ISharedObjectListener;

class SharedObject : public virtual ISerializable
 {
	WOSH_CLASSNAME(wosh::SharedObject)
	WOSH_CLASS_ABSTRACT(true)

	public:
		enum OPERATION {
			OBJ_UNKNOWN	= 0x00,	///< undefined, for initialization
			OBJ_CREATED	= 0x01,	///< 
			OBJ_UPDATED	= 0x02,	///< 
			OBJ_DELETED	= 0x08
		 };

	public:
		/**
		 * @brief  Empty constructor.
		 */
		SharedObject()
			: flags(0), sho_listener(NULL) { }
		/**
		 * @brief  Empty destructor.
		 */
		virtual ~SharedObject() { }

	public:

/** @name SharedObject interface
 * @{
 ******************************************************************************/
	public:
		virtual const std::string& getSharedID() const = 0;

	protected:
//		virtual void updated( const SharedObject* object ) = 0;


	private:
//		virtual void updated_friend( const SharedObject* object ) { updated(object); }
 
//		template<class> friend class wosh::SharedMemory;
//@}
/** @name Serialization Interface
 * @{
 ******************************************************************************/
	public:
		virtual bool updateHash( DataHashContext* hash_context ) const;
//@}

	protected:
		URI dsmPrimary; ///< the DSM agent flagged as primary for THIS object
		int flags;		///< bitmask flags: read-only/RW/ ?

		std::map<long, int64> vector_clock;

		ISharedObjectListener* sho_listener;

		friend class SharedMemory;

}; // class def






//------------------------------------------------------------------------------------











class SharedObjectSkeleton;




class ISharedObject : public virtual ISerializable
 {
	WOSH_CLASSNAME(wosh::SharedObject)
	WOSH_CLASS_ABSTRACT(true)

	public:
		enum OPERATION {
			OBJ_UNKNOWN	= 0x00,	///< undefined, for initialization
			OBJ_CREATED	= 0x01,	///< 
			OBJ_UPDATED	= 0x02,	///< 
			OBJ_DELETED	= 0x08
		 };

	public:
		/**
		 * @brief  Empty constructor.
		 */
		ISharedObject()
			: sho_listener(NULL) { }
		/**
		 * @brief  Empty destructor.
		 */
		virtual ~ISharedObject();

	public:
		virtual const std::string& getSharedID() const = 0;


	protected:
		ISharedObjectListener* sho_listener;

		friend class SharedMemory;
		friend class SharedObjectSkeleton;

}; // class def




class ISharedObjectListener  {
	public:
		virtual void sobj_updated( SharedObject* source, ISerializable* update_data ) = 0;
		virtual void sobj_destroying( ISharedObject* source ) = 0;

	public:
		virtual ~ISharedObjectListener() { }

}; // class def






class SharedObjectSkeleton : public virtual ISerializable
 {
	WOSH_CLASSNAME(wosh::SharedObjectSkeleton)
	WOSH_CLASS_ABSTRACT(false)

	public:
		/**
		 * @brief  Empty constructor.
		 */
		SharedObjectSkeleton()
			: object(NULL), primaryID(0) { }
		/**
		 * @brief  Empty destructor.
		 */
		virtual ~SharedObjectSkeleton();

	public:
		inline const std::string& getSharedID() const {
			if ( object == NULL ) return emptyString;
			return object->getSharedID();
		 }


/** @name Serialization Interface
 * @{
 ******************************************************************************/
	public:
		virtual inline std::string toString() const				{ return getSharedID(); }
		virtual bool updateHash( DataHashContext* hash_context ) const;
//@}
	protected:
		ISharedObject* object;
		long primaryID;

		friend class SharedMemory;
		friend class ISharedObject;

		static std::string emptyString;

}; // class def









}; // namespace wosh

#endif //__WOSH_Core_SharedObject_H__
